<?php
namespace Home\Controller;
use Think\Controller;
class TableController extends CommonController {

    public function index($tb = ''){
        $Table = D('Table');
        $tables = $Table->getTables();
        $this->assign('tables', $tables);
        $table = $Table->getInfo($tb);
        $this->assign('tb', $tb);
        $this->assign('table_count', count($table)-1);
        $this->assign('table', $table);
        $this->display();
    }

    public function opt($setup ='',$tb = '')
    {
        $Model = M();
        if ($setup == 'analyze') {
            $sql = "ANALYZE TABLE `".$tb."`;";
            $Model->execute($sql);
            $this->success($sql);
        }elseif ($setup == 'optimize') {
            $sql = "OPTIMIZE TABLE `".$tb."`;";
        }elseif ($setup == 'check') {
            $sql = "CHECK TABLE `".$tb."`;";
        }elseif ($setup == 'repair') {
            $sql = "REPAIR TABLE `".$tb."`;";
        }elseif ($setup == 'truncate') {
            $sql = "TRUNCATE TABLE `".$tb."`;";
        }
        if ($sql) {
            $Model->execute($sql);
            $this->success($sql);
        }else{
            $this->error('未做任何修改');
        }
    }

    public function copyFrom($scene ='',$source = 'Common')
    {
        if (IS_POST) {
            $i = F($this->tb,'',C('RUN_PATH'));
            $s = $i[$source];
            $i[$scene] = $s;
            F($this->tb,$i,C('RUN_PATH'));
            $this->success('已成功复制场景');
        }else{
            $this->display();
        }
    }

    public function design($scene ='Common')
    {
        $Table = D('Table');
        $table = $Table->getInfo($this->tb);
        $table = getTbFormField($table,$scene);
        $scenes = getAllFormScene($this->tb);
        $this->assign('table', $table);
        $this->assign('scene', $scene);
        $this->assign('scenes', $scenes);
        $this->assign('table_count', count($table)-1);
        
        $this->display();
    }

    public function updateField($tb = '',$name ='',$pk ='',$value = '')
    {
        $Table = D('Table');
        if ($name == 'default') {
            $Table->saveFieldDef($tb,$pk,$value);
        }elseif ($name == 'isnull') {
            $Table->saveFieldIsNull($tb,$pk,$value);
        }elseif ($name == 'length') {
            $Table->saveFieldLength($tb,$pk,$value);
        }elseif ($name == 'moveUp' || $name == 'moveDown') {
            $Table->saveFieldMove($tb,$pk,$name);
        }elseif ($name == 'comment') {
            $Table->saveFieldComment($tb,$pk,$value);
        }

    }

    public function updateFrom($tb = '',$name ='',$pk ='',$value = '',$scene = 'Common')
    {
        $info = F($tb,'',C('ITEM_PATH'));
        // print_r($info);exit();
        if (!$info) $this->error('修改失败');
        $value = str_replace("：", ":", $value);
        $info[$scene][$pk][$name] = $value;
        F($tb,$info,C('ITEM_PATH'));
        $this->success('修改成功');
    }
    public function deleteFrom($tb = '',$name ='',$pk ='',$value = '')
    {
        if ($pk == 'all') {
            F(C('ITEM_PATH').$tb,NULL);
            $this->success('修改成功');
        }
        $info = F($tb,'',C('ITEM_PATH'));
        if (!$info) $this->error('修改失败');
        unset($info['Common'][$pk]);
        F(C('ITEM_PATH').$tb,$info);
        $this->success('修改成功');
    }

    public function addField()
    {
        if (IS_POST) {
            $Table = D('Table');
            $Table->addField($_POST);
            $this->success('操作成功');
        }else {
            $this->display();
        }
    }

    public function deleteField($tb ='',$field = '')
    {
        $Table = D('Table');
        $Table->deleteField($tb,$field);
        $this->success('操作成功');
    }

    
}
